class Game {
    constructor(canvasId) {
        this.canvas = document.getElementById(canvasId);
        this.ctx = this.canvas.getContext('2d');
        this.gridSize = 20;
        this.tileCount = this.canvas.width / this.gridSize;
        this.score = 0;
        this.gameSpeed = 150;
        this.gameRunning = false;
        this.gameLoopTimeout = null;
        
        this.snake = new Snake();
        this.food = new Food(this.tileCount);
    }

    start() {
        this.reset();
        this.gameRunning = true;
        this.draw();
        this.gameLoop();
    }

    reset() {
        this.snake.reset();
        this.food.reset(this.snake.segments);
        this.score = 0;
        this.updateScoreDisplay();
    }

    gameLoop() {
        if (this.gameRunning) {
            this.gameLoopTimeout = setTimeout(() => {
                this.update();
                this.draw();
                this.gameLoop();
            }, this.gameSpeed);
        }
    }

    update() {
        this.snake.updateDirection();
        const newHead = this.snake.move();
        
        // 检查是否吃到食物
        if (newHead.x === this.food.position.x && newHead.y === this.food.position.y) {
            this.score++;
            this.updateScoreDisplay();
            this.food.reset(this.snake.segments);
        } else {
            this.snake.segments.pop();
        }
        
        // 检查游戏结束条件
        if (this.checkGameOver()) {
            this.gameOver();
        }
    }

    checkGameOver() {
        const head = this.snake.segments[0];
        
        // 检查是否撞墙
        if (head.x < 0 || head.x >= this.tileCount || 
            head.y < 0 || head.y >= this.tileCount) {
            return true;
        }
        
        // 检查是否撞到自己
        return this.snake.checkSelfCollision();
    }

    draw() {
        // 清空画布
        this.ctx.fillStyle = '#34495e';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
        
        // 绘制网格线
        this.drawGrid();
        
        // 绘制食物
        this.drawFood();
        
        // 绘制蛇
        this.drawSnake();
    }

    drawGrid() {
        this.ctx.strokeStyle = '#2c3e50';
        this.ctx.lineWidth = 0.5;
        for (let i = 0; i < this.tileCount; i++) {
            this.ctx.beginPath();
            this.ctx.moveTo(i * this.gridSize, 0);
            this.ctx.lineTo(i * this.gridSize, this.canvas.height);
            this.ctx.stroke();
            
            this.ctx.beginPath();
            this.ctx.moveTo(0, i * this.gridSize);
            this.ctx.lineTo(this.canvas.width, i * this.gridSize);
            this.ctx.stroke();
        }
    }

    drawSnake() {
        this.snake.segments.forEach((segment, index) => {
            if (index === 0) {
                // 绘制蛇头
                const gradient = this.ctx.createLinearGradient(
                    segment.x * this.gridSize, 
                    segment.y * this.gridSize, 
                    segment.x * this.gridSize + this.gridSize, 
                    segment.y * this.gridSize + this.gridSize
                );
                gradient.addColorStop(0, '#2ecc71');
                gradient.addColorStop(1, '#27ae60');
                
                this.ctx.fillStyle = gradient;
                this.ctx.beginPath();
                this.ctx.roundRect(
                    segment.x * this.gridSize + 1, 
                    segment.y * this.gridSize + 1, 
                    this.gridSize - 2, 
                    this.gridSize - 2, 
                    5
                );
                this.ctx.fill();
                
                // 蛇眼
                this.ctx.fillStyle = 'white';
                this.ctx.beginPath();
                this.ctx.arc(
                    segment.x * this.gridSize + this.gridSize/3, 
                    segment.y * this.gridSize + this.gridSize/3, 
                    2, 
                    0, 
                    Math.PI * 2
                );
                this.ctx.arc(
                    segment.x * this.gridSize + this.gridSize*2/3, 
                    segment.y * this.gridSize + this.gridSize/3, 
                    2, 
                    0, 
                    Math.PI * 2
                );
                this.ctx.fill();
            } else {
                // 绘制蛇身
                this.ctx.fillStyle = `rgb(46, 204, 113, ${1 - index * 0.01})`;
                this.ctx.beginPath();
                this.ctx.roundRect(
                    segment.x * this.gridSize + 1, 
                    segment.y * this.gridSize + 1, 
                    this.gridSize - 2, 
                    this.gridSize - 2, 
                    3
                );
                this.ctx.fill();
            }
        });
    }

    drawFood() {
        // 绘制食物
        this.ctx.fillStyle = '#e74c3c';
        this.ctx.beginPath();
        this.ctx.arc(
            this.food.position.x * this.gridSize + this.gridSize/2, 
            this.food.position.y * this.gridSize + this.gridSize/2, 
            this.gridSize/2 - 2, 
            0, 
            Math.PI * 2
        );
        this.ctx.fill();
        
        // 食物光泽效果
        this.ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
        this.ctx.beginPath();
        this.ctx.arc(
            this.food.position.x * this.gridSize + this.gridSize/2 + 2, 
            this.food.position.y * this.gridSize + this.gridSize/4, 
            3, 
            0, 
            Math.PI * 2
        );
        this.ctx.fill();
    }

    updateScoreDisplay() {
        document.getElementById('score-display').textContent = `得分: ${this.score}`;
    }

    gameOver() {
        this.gameRunning = false;
        clearTimeout(this.gameLoopTimeout);
        document.getElementById('final-score').textContent = `得分: ${this.score}`;
        document.getElementById('game-over').style.display = 'flex';
    }

    setSpeed(speed) {
        this.gameSpeed = speed;
    }
}